﻿@model IEnumerable<MVC3Demo.Models.Item>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
    @Html.TextBox("search")
    <input type="submit" id="btnSearch" value="Search" />
}
<table width="820px">
    <tr>
        <th>
            Message
        </th>
        <th>
            Created
        </th>
        <th>
            Action
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Message)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Created)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id, returnPage = @ViewContext.RouteData.Values["page"] })
                @Html.ActionLink("Details", "Details", new { id = item.Id, returnPage = @ViewContext.RouteData.Values["page"] })
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }
    <tr>
        <td colspan="3" class="pagerSummary">
            @{
                int pageCount = (int)Math.Ceiling((float)ViewBag.ItemCount / ViewBag.PageSize);
                int currentPage = int.Parse(@ViewContext.RouteData.Values["page"].ToString());
                string searchString = Request.QueryString["search"];
            }
            Page @ViewContext.RouteData.Values["page"] / @pageCount (@ViewBag.ItemCount items)
            @Html.ActionLink("Previous", "Index", new { page = currentPage - 1, search = searchString })
            @Html.ActionLink("Next", "Index", new { page = currentPage + 1, search = searchString })
        </td>
    </tr>
    <tr>
        <td colspan="3">
            for (int i = 1; i <= pageCount; i++)
            @for (int i = ((currentPage - 10) < 1) ? 1 : currentPage - 10; i <= ((pageCount < 10) ? pageCount : currentPage + 10); i++)
            {
                if (i == currentPage)
                {
                <div class="pager current">
                    @Html.ActionLink(i.ToString(), "Index", new { page = i, search = searchString })
                </div>
                }
                else
                {
                <div class="pager">
                    @Html.ActionLink(i.ToString(), "Index", new { page = i, search = searchString })
                </div>
                }
            }
        </td>
    </tr>
</table>
